package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.Servicedetails;
import com.heima.homeservicemanagementsystem.service.ServicedetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.UserFavoritesService;
import com.heima.homeservicemanagementsystem.service.UserFavoritesServiceService;

import java.util.ArrayList;
import java.util.List;

import static cn.dev33.satoken.SaManager.log;

/**
 * 用户收藏服务;(user_favorites_service)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-17
 */
@Tag(name = "用户收藏服务对象功能接口")
@RestController
@RequestMapping("/userFavoritesService")
public class UserFavoritesServiceController {

    @Resource
    private UserFavoritesServiceService userFavoritesServiceService;
    @Resource
    private ServicedetailsService servicedetailsService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<Servicedetails>> paginQuery(@RequestBody PageParams<UserFavoritesService> pageParams) {
        //查询参数
        UserFavoritesService params = pageParams.getParams();
        LambdaQueryWrapper<UserFavoritesService> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasLength(params.getUserId()), UserFavoritesService::getUserId, params.getUserId());
        Page<UserFavoritesService> page = userFavoritesServiceService.page(pageParams.getPage(), lqw);
        List<UserFavoritesService> records = page.getRecords();
        ArrayList<Servicedetails> objects = new ArrayList<>();
        for (UserFavoritesService record : records) {
            Servicedetails byId = servicedetailsService.getById(record.getServiceId());
            QueryWrapper<UserFavoritesService> wrapper = new QueryWrapper<>();
            wrapper.eq("service_id", record.getServiceId());
            long count = userFavoritesServiceService.count(wrapper);
            byId.setScNumber((int) count);
            byId.setGlId(record.getId());
            objects.add(byId);
        }
        Page<Servicedetails> objectPage = new Page<>();
        objectPage.setRecords(objects);
        objectPage.setTotal(page.getTotal());
        objectPage.setPages(page.getPages());
        objectPage.setCurrent(page.getCurrent());
        objectPage.setSize(page.getSize());
        return Result.success(objectPage);
    }


    @Operation(summary = "用户收藏服务所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        return Result.success(userFavoritesServiceService.list());
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody UserFavoritesService userFavoritesService) {
        LambdaQueryWrapper<UserFavoritesService> lqw = new LambdaQueryWrapper<>();


        return Result.success(userFavoritesServiceService.list(lqw));
    }

    /**
     * 新增数据
     *
     * @param userFavoritesService 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result insert(@RequestBody UserFavoritesService userFavoritesService) {
        log.info("前端信息：{}", userFavoritesService);

        if (userFavoritesServiceService.save(userFavoritesService)) {
            String id = userFavoritesService.getId();
            log.info("Id信息：{}", id);

            return Result.success(id);
        }
        return Result.error();
    }

    @Operation(summary = "根据用户的id还有就是商品的id查看是否存在然后删除")
    @PostMapping("/deletesc")
    public Result<Boolean> deletesc(@RequestBody UserFavoritesService userFavoritesService) {
        log.info("前端信息：{}", userFavoritesService);
//        if (userFavoritesServiceService.save(userFavoritesService)) {
//            return Result.success();
//        }
        QueryWrapper<UserFavoritesService> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userFavoritesService.getUserId());
        wrapper.eq("service_id", userFavoritesService.getServiceId());
        boolean exists = userFavoritesServiceService.exists(wrapper);
        log.info("是否存在：{}", exists);
        if (exists) {
            boolean remove = userFavoritesServiceService.remove(wrapper);
            return Result.success(remove);
        }

        return Result.error("false");
    }

    @Operation(summary = "查看是否已经收藏")
    @PostMapping("/getscInfo")
    public Result<Boolean> getscInfo(@RequestBody UserFavoritesService userFavoritesService) {
        log.info("前端信息：{}", userFavoritesService);
//        if (userFavoritesServiceService.save(userFavoritesService)) {
//            return Result.success();
//        }
        QueryWrapper<UserFavoritesService> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userFavoritesService.getUserId());
        wrapper.eq("service_id", userFavoritesService.getServiceId());
        boolean exists = userFavoritesServiceService.exists(wrapper);
        log.info("是否存在：{}", exists);
        if (exists) {
            return Result.success(exists);
        }

        return Result.error("false");
    }

    /**
     * 更新数据
     *
     * @param userFavoritesService 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody UserFavoritesService userFavoritesService) {
        if (userFavoritesServiceService.updateById(userFavoritesService)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param undefinedId 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "undefinedId", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{undefinedId}")
    public Result<Boolean> deleteById(@PathVariable String undefinedId) {

        if (userFavoritesServiceService.removeById(undefinedId)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (userFavoritesServiceService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }
}